const mongoose=require("mongoose");
//数据库连接
const db= mongoose.createConnection('mongodb://localhost:27017/stu',{useNewUrlParser:true, useUnifiedTopology: true});

mongoose.Promise = global.Promise;
db.on('error',(err)=>{console.log(err)});
db.on('open',()=>{console.log('mongodb连接成功')});
const Schema = mongoose.Schema;   //得到Schema构造函数
const StuSchema = new Schema({   //用Schema定义插入数据的类型
    name:{
        type:String,
        required:true
    },
    age:Number,
    sex:{
        type:String,
        required:true
    },
    address:{
        type:String,
        required:true
    },
    identity:{
        type:String,
        required:true
    },
    tel:{
        type:String,
        required:true
    }
});
const UserSchema = new Schema({   //用Schema定义插入数据的类型
    usr:{
        type:String,
        required:true
    },
    pwd:{
        type:String,
        required:true
    },
});
const ShopSchema = new Schema({   //用Schema定义插入数据的类型
    num:{ //商品编号
        type:String,
        required:true
    },
    shop_name:{
        type:String,
        required:true
    },
    stock:{ //商品库存
        type:Number,
        required:true
    },
    type:{ //商品类型
        type:String,
        required:true
    },
    price:{ //商品价格
        type:String,
        required:true
    },
    count:{ //商品数量
        type:Number,
        required:true
    },
});
//建立集合，规定使用定义好的Shcema标准插入数据，返回一个构造函数
const Stu = db.model('stu',StuSchema,"stu");
const User = db.model('user',UserSchema,"user");
const Shop = db.model('shop',ShopSchema,"shop");
//将Users暴露出去，可以用于操作数据
exports.Stu = Stu;
exports.User=User;
exports.Shop=Shop;